import { createDiscreteApi } from 'naive-ui'
import { storeToRefs } from 'pinia'
// import { authVariable } from '@/stores/useAuthStore'
import { flat } from '@/utils/tool'

const { message, notification, loadingBar, dialog } = createDiscreteApi([
  'message',
  'dialog',

])
const whiteList = ['/login'] // no redirect whitelist
export const routerGuard = (router) => {
  router.beforeEach(async (to, from, next) => {
    // const { userInfo, userAuthInfo, routerTree } = storeToRefs(authVariable())
    // const { getMenuRouter, logout } = authVariable()
    // const token = userInfo.value?.accessToken
    // if ('/login'.includes(to.path)) {
      next()
    // }
    // else if (!(whiteList.includes(to.path)) && token) { // 其他页面访问且有token
    //   const hasRouteAuth = flat(routerTree.value)?.filter(item => item.path.includes(to.path))?.length > 0
    //   console.log('其他页面且登录过-放行(当前to的路径是否在该用户的路由表中)', hasRouteAuth)
    //   if (hasRouteAuth) {
    //     if (!to.matched?.length) { // 没匹配到且当前to的路径是在该用户的路由表中
    //       await getMenuRouter()
    //       next({ path: to.path })
    //     }
    //     else { // 匹配到了放行
    //       next()
    //     }
    //   }
    //   else {
    //     message.info('未匹配到该路径，请重新登录！')
    //     next({ path: '/login', replace: true })
    //   }
    // }
    // else if (!(whiteList.includes(to.path)) && !token) { // 没登录直接访问系统
    //   message.warning('账号未分配菜单权限，请重新登录并联系管理员！', { duration: 3500 })
    //   next({ path: '/login', replace: true })
    // }
    // else {
    //   next({ path: '/login', replace: true })
    // }
  })
}
